package com.java46.carera.stock.outstock.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.FetchStorage;
import com.java46.carera.base.entity.Repertory;
import com.java46.carera.base.entity.vo.FetchStorageVo;
import com.java46.carera.base.exception.BusinessException;
import com.java46.carera.base.mapper.FetchStorageMapper;
import com.java46.carera.base.mapper.RepertoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName OutStockService
 * @Description TODO
 * @Author Administrator
 * @Date 2018/8/23 0023 19:43
 * @Vcrsion 1.0
 **/
@Service
public class OutStockService {

    @Autowired
    private FetchStorageMapper fetchStorageMapper;

    @Autowired
    private RepertoryMapper repertoryMapper;


    /**
     * @Author xiao
     * @Description //分页查询出库记录可关键字查询
     * @Date 22:54 2018/8/23 0023
     * @Param [pageNum, pageSize, keywords]
     * @return com.github.pagehelper.PageInfo<com.java46.carera.base.entity.vo.FetchStorageVo>
     */
    public PageInfo<FetchStorageVo> queryOutStock(int pageNum, int pageSize, String keywords) throws Exception {

        PageHelper.startPage(pageNum,pageSize);
        List<FetchStorageVo> repertories = fetchStorageMapper.selectAll(keywords);
        if (repertories.isEmpty()){
            throw new BusinessException("没有查询到信息");
        }
        PageInfo<FetchStorageVo> pages = new PageInfo<>(repertories);
        return pages;
    }


    /**
     * @Author xiao
     * @Description //新增出库记录
     * @Date 22:55 2018/8/23 0023
     * @Param [fetchStorage]
     * @return void
     */
    public void insertOutStock(FetchStorage fetchStorage) throws Exception {

        if(fetchStorage==null){
            throw new BusinessException("没有任何出库信息");
        }
//        查询出库配件信息
        Repertory repertory = repertoryMapper.selectByRepid(fetchStorage.getRepid());
        if(repertory.getRepnum()<fetchStorage.getNumber()){
            throw new BusinessException("库存数量不足");
        }
        fetchStorageMapper.insertFetchStorage(fetchStorage);
        repertoryMapper.updateOutRepNum(fetchStorage.getNumber(),fetchStorage.getRepid());
    }

    /**
     * @Author xiao
     * @Description //通过出库编号删除出库记录
     * @Date 22:56 2018/8/23 0023
     * @Param [fetId]
     * @return void
     */
    public void deleteOutStock(Integer fetId) throws Exception {

        if(fetId==null){
            throw new BusinessException("没有任何出库信息");
        }
        fetchStorageMapper.deleteByFetId(fetId);
    }
}
